//window.onhashchange = function() {
//    if (location.hash != ""){
//
//    }
//
//}
$(document).ready(function() {
    $("#btnFollow").click(function() {
        var followid = getURLParam("id");
        if(followid != ""){
            var str_string = 'flag=followUser&followid='+ followid;
            $.ajax({
                type: "POST",
                url: "./BLL/friendListBll.php",
                data: str_string,
                cache: false,
                success: function(dto) {
                    if(dto.trim() == 'success'){
                        $("#btnFollow").addClass("following");
                    } else {
                        bootbox.alert("At the moment, You can't follow this user. Please try again!");
                    }
                }
            });
        } else{
            bootbox.alert("Can not get information's user who you want follow.");
        }
    });
    $("#btnAddFriend").click(function() {
        var friendid = getURLParam("id");
        if(friendid != ""){
            var str_string = 'flag=addFriend&friendid='+ friendid;
            $.ajax({
                type: "POST",
                url: "./BLL/friendListBll.php",
                data: str_string,
                cache: false,
                success: function(dto) {
                    if(dto.trim() == 'success'){
                        $("#btnAddFriend").addClass("disableButton");
                    } else {
                        bootbox.alert("At the moment, You can't add friend this user. Please try again!");
                    }
                }
            });
        } else{
            bootbox.alert("Can not get information's user who you want add friend.");
        }
    });
});
function getURLParam( name ){
    var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

$(function () {


    $("#createMessage").click(function (){
        window.location.href = 'message.php';
    });

	$(".menu-group").each(function () {
		$(this).hover(function () {
			blurAllMenu();
			$(this).addClass("hover");
			setMenuHover(this);
			$("#menu-popup").get(0).className = "menu-popup " + (this.id);
			$("#menu-popup").addClass("active");
			$("#menu-popup").css({overflow: "visible"}).stop().animate({width: 350}, 1000, "easeOutQuint");
			$(".menu-popup-form." + this.id + " .menu-popup-list").css({right: 350}).animate({right: 0}, 1100, "easeOutQuint");
			removeCalendar();
		}, function () {
		});

	});

	$("#menu-popup").hover(function () {
		$(this).addClass("active");
		$(this).stop().animate({width: 350});
	}, function () {
		blurAllMenu();
		$(this).stop().animate({width: 0}, "easeOutQuint", function () {
			$(this).removeClass("active");
			removeCalendar();
		});
	});
	$(".metro-item-more").hover(function () {
		$(this).stop().animate({height: 120, backgroundColor: "#062E68"}, 1000, "easeOutQuint");
	}, function () {
		$(this).stop().animate({height: 80, backgroundColor: "#0046A9"}, 1000, "easeOutQuint");
	});
	$(".form-head-search").hover(function () {
		$(this).stop().animate({width: 400}, 1000, "easeOutQuint");
		$(".search-text").focus();
	}, function () {
		$(this).stop().animate({width: 80}, 1000, "easeOutQuint");
	});$(".form-head-account").hover(function () {
		$(".form-head-account-wrapper").stop().animate({top: -90}, 1000, "easeOutQuint");
	}, function () {
		$(".form-head-account-wrapper").stop().animate({top: 0}, 1000, "easeOutQuint");
	});
	loadMetroPage();
	$(".popup-back").click(function () {
		hideLogin();
		resetForm();
	});

	$("#login-info-link-register").click(function () {
		$(".popup-form").css({height: "410px"});
		changeLogin("register");
	});
	$("#btnLogin").click(function () {
		$(".popup-form").css({height: "350px"});
		showLogin("login");
	});
	$("#btnRegis").click(function () {
		$(".popup-form").css({height: "410px"});
		showLogin("register");
	});
	$(".video-control-metro.large.scroll").click(function () {
		$(this).toggleClass("off")
	});
	resetCalendarDate();
	resetCalendarWeek();
	initScrollBar();
	initMessage();
});
var fbUserData = null;
function getFbUserData(callback) {
	callback = callback || function () {
	};
	if (fbUserData == null) {
		FB.api('/me', function (response) {
			if (!response || response.error) {
				console.log('error get user data');
				console.log(response);
			} else {
				fbUserData = response;
				callback();
			}
		});
	}
	return fbUserData;

}
function onFacebookReady() {
	if (video != null) {
		postPlay(video);
	}
}
function postPlay(video) {
	FB.api(
		'me/listentomeapp:play',
		'post',
		{
			other: video.website_url,
			score: 1000,
			title: video.title
		},
		function (response) {
			// handle the response
			console.log(response);
		}
	);
}
function loadMetroPage() {
	var metroIndex = 0;
	$(".metro-item").each(function () {
		$(this).css({left: 500, opacity: 0}).delay(metroIndex++ * 50).animate({left: 0, opacity: 1}, 2000, "easeOutQuint");
	});
	resetMetroHover();
}
function resetMetroHover() {
	$(".metro-item").hover(function () {
		$(this).children(".metro-bar").animate({height: 200}, 500, "easeOutQuint");
	}, function () {
		$(this).children(".metro-bar").animate({height: 90}, 500, "easeOutQuint");
	});
}
function setMenuHover(item) {
	$(item).children(".menu-arrow").stop().animate({right: 0}, 200);
	$(item).stop().animate({backgroundColor: "#222222"}, 200);

}
function setMenuBlur(item) {
	$(item).children(".menu-arrow").stop().animate({right: -20}, 200);
	$(item).stop().animate({backgroundColor: "#111111"}, 200);

}
function blurAllMenu() {
	$(".menu-item").each(function () {
		$(this).removeClass("hover");
		setMenuBlur(this);
	});
}
function showLogin(id) {
	$(".popup").removeClass("disable");
	$(".popup").css({opacity: 0}).animate({opacity: 1}, 250);
	$(".popup-wrapper").css({opacity: 0});
	$(".popup-form." + id).css({width: 0}).animate({width: "60%"}, 1000, "easeOutQuint");
	$(".popup-form." + id + " .popup-wrapper").delay(300).animate({opacity: 1}, 250);
	$(".popup-form." + id + " .login-info-input").first().focus();
	if (id == "login") {
		$(".register .popup-wrapper").css({display: 'none'});
		$(".login .popup-wrapper").css({display: 'initial'});
	}
	else {
		$(".login .popup-wrapper").css({display: 'none'});
		$(".register .popup-wrapper").css({display: 'initial'});
	}
}
function hideLogin() {
	$(".popup-wrapper").animate({opacity: 0}, 250);
	$(".popup-form").animate({width: 0}, 1000, "easeOutQuint");
	$(".popup").delay(300).animate({opacity: 0}, 250, function () {
		$(".popup").addClass("disable");
	});
}
function changeLogin(id) {
	$(".popup-wrapper").animate({opacity: 0}, 250);
	$(".popup-form").animate({width: 0}, 1000, "easeOutQuint");
	$(".popup").delay(300).animate({opacity: 0.5}, 250, function () {
		showLogin(id);
	});
}
function resetForm() {
	$(".login-info-input")[0].value = "";
	$(".login-info-input")[1].value = "";
	$(".login-info-input")[2].value = "";
	$(".login-info-input")[3].value = "";
	$(".login-info-input")[4].value = "";
	$(".login-info-input")[5].value = "";
	$(".login-info-radio")[0].checked = true;
	$(".login-info-row")[4].className = "login-info-row";
	$(".login-info-row")[5].className = "login-info-row";
	$(".login-info-row")[6].className = "login-info-row";
	$(".login-info-row")[7].className = "login-info-row";
	$(".login-message-content").addClass("undisplayed");
	$(".login-message-footer").addClass("undisplayed");
}

function resetCalendarDate() {
	setTimeout(function () {
		$('.date-pick').datePicker({
			closeOnSelect: true,
			startDate: "1/1/1980",
			onSelect: function (value) {
				//alert(value.getDate()+"/"+(value.getMonth()+1)+"/"+value.getFullYear());
				var param1 = value.getFullYear() + "/" + (value.getMonth() + 1) + "/" + value.getDate();
				getRankingByDate(param1, "1");
			},
			showYearNavigation: true, startDate: "1/1/1980"});
	}, 1500);
}

function resetCalendarWeek() {
	setTimeout(function () {
		$('.date-pick.week').datePicker({selectWeek: true,
			closeOnSelect: true,
			startDate: "1/1/1980",
			onSelect: function (value) {
				var endDate = new Date(value.getTime() + 6 * (24 * 60 * 60 * 1000));
				var date = value.getDate() + "/" + (value.getMonth() + 1) + "/" + value.getFullYear();
				var sdate = endDate.getDate() + "/" + (endDate.getMonth() + 1) + "/" + endDate.getFullYear();
				$('.date-pick.week').val(date + " - " + sdate);
				var param1 = value.getFullYear() + "/" + (value.getMonth() + 1) + "/" + value.getDate();
				var param2 = endDate.getFullYear() + "/" + (endDate.getMonth() + 1) + "/" + endDate.getDate();
				getRankingByDate(param1, param2);
			}
		});
	}, 1500);
}

function removeCalendar() {
	$('#dp-popup').remove(false);
}

function readURL(input, thumbimage) {
	if (input.files && input.files[0]) {
		var reader = new FileReader();
		reader.onload = function (e) {
			$("#thumbimage").attr('src', e.target.result);
//            $("#thumbimage").css({background: "url('"+e.target.result+"') no-repeat center"});
		}
		reader.readAsDataURL(input.files[0]);
		$("#thumbimage").show();
	}
	else {
		$("#thumbimage").attr('src', input.value);
		$("#thumbimage").show();
	}
	$(".removeimg").show();
}
function initScrollBar() {
	$(window).load(function () {
		$(".control-rank-list").mCustomScrollbar({
			autoHideScrollbar: true,
			theme: "light-thin"
		});
		$(".play-exam").mCustomScrollbar({
			autoHideScrollbar: false,
			theme: "dark-thin",
                        advanced:{
                            autoScrollOnFocus: true,
                            updateOnContentResize: true
                        }
		});

        $("#messageList").mCustomScrollbar({
            autoHideScrollbar: false,
            theme: "light-thin",
            advanced: {updateOnContentResize: true}
        });
                
		$("#popup-category-list").mCustomScrollbar({
			autoHideScrollbar: false,
			theme: "light-thin",
			advanced: {updateOnContentResize: true}
		});

		$("#rank-list-week").mCustomScrollbar({
			autoHideScrollbar: false,
			theme: "light-thin",
			advanced: {updateOnContentResize: true}
		});
		$("#rank-list-day").mCustomScrollbar({
			autoHideScrollbar: false,
			advanced: {updateOnContentResize: true },
			theme: "light-thin"
		});
	});
}
function initMessage() {
	$(".contact-item").click(function(){
		$(".contact-item").removeClass("active");
		$(this).addClass("active");
	})
	$(".emo-item").click(function(){
		$("#message-input").append($(this).text());
	})
}